package cn.zhf.shop.service.impl;

import cn.zhf.shop.entity.Campus;
import cn.zhf.shop.entity.ConsoleUser;
import cn.zhf.shop.entity.lesson.Lesson;
import cn.zhf.shop.entity.Message;
import cn.zhf.shop.entity.organization.Organization;
import cn.zhf.shop.entity.search.LessonSearch;
import cn.zhf.shop.mapper.CampusMapper;
import cn.zhf.shop.mapper.ConsoleUserMapper;
import cn.zhf.shop.mapper.LessonMapper;
import cn.zhf.shop.mapper.LessonTypeMapper;
import cn.zhf.shop.service.LessonService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Component
public class LessonServiceImpl implements LessonService {

    @Resource
    private LessonMapper lessonMapper;

    @Resource
    private CampusMapper campusMapper;

    @Resource
    private LessonTypeMapper lessonTypeMapper;

    @Resource
    private ConsoleUserMapper consoleUserMapper;

    @Override
    public Message<?> insertLesson(Lesson l) {
        /*
         * 文件上传流程：
         * 客户端请求上传文件，文件上传保存在临时文件夹，返回文件名
         * 课程表单提交时，携带后台返回的文件名，后台从临时文件夹中将文件移动到业务目录
         * 定时任务，每天凌晨三点清空临时文件夹的所有内容
         */

        //注：文件夹名可自拟


        //提取封面图片名称（因为上传时会给每个图片名后面加上分号用于分隔）
//        File titleImg = new File(config.globalFilePath+"/temp/"+news.getNewsTitleImg().split(";")[0]);
//
//       将标题图片从临时文件夹转移至业务文件夹
//        if (titleImg.exists()) {
//            boolean b = titleImg.renameTo(new File(config.globalFilePath + "/news/" + news.getNewsTitleImg()));
//            logger.info("将标题图片转移至业务文件夹，标题：{}结果：{}",news.getNewsTitle(),b);

//            将标题图片设置为外部加域名后可访问到的URL
//            news.setNewsTitleImg("/resource/news/"+news.getNewsTitleImg());
//        }else{
//            logger.warn("标题图片不存在！标题：{}，目标位置：{}",news.getNewsTitle(),titleImg.getAbsolutePath());
//        }
//
//        通过split函数将分号间隔的文件名解析为数组，遍历数组，依次将文件从临时文件夹转移至业务文件夹
//        for (String fileName : news.getNewsContentImg().split(";")) {
//            File tmpImg = new File(config.globalFilePath+"/temp/"+fileName);
//            if (tmpImg.exists()) {
//                tmpImg.renameTo(new File(config.globalFilePath+"/news/"+fileName));
//
//         将内容图片设置为外部加域名后可访问到的URL，并使用分号进行分割
//                news.setNewsContentImg(news.getNewsContentImg()+"/resource/news/"+fileName+";");
//            }
//        }

        return null;

    }

    @Override
    public Message deleteByPrimaryKey(Integer id) {
        return Message.success(null).add(lessonMapper.deleteByPrimaryKey(id));
    }

    @Override
    public Message insert(Lesson record) {
        return lessonMapper.insert(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<Lesson> selectByPrimaryKey(Integer id) {
        Lesson refund = lessonMapper.selectByPrimaryKey(id);
        return refund == null ? Message.fail("无记录") : Message.success(null).add(refund);
    }

    @Override
    public Message updateByPrimaryKey(Lesson record) {
        return lessonMapper.updateByPrimaryKey(record) > 0 ? Message.success("更新成功" + record.getLessonId()) : Message.fail("更新失败");
    }

    @Override
    public Message<List<Lesson>> selectAll() {
        List<Lesson> refund = lessonMapper.selectAll();
        return refund == null ? Message.fail("无记录") : Message.success(null).add(refund);
    }

    @Override
    public Message<List<Lesson>> selectOverviewByName(String lessonName) {
        List<Lesson> refund = lessonMapper.selectOverviewByName(lessonName);
        return refund == null ? Message.fail("无记录") : Message.success(null).add(refund);
    }

    @Override
    public Message<List<Lesson>> selectOverviewByType(String lessonType) {
        List<Lesson> refund = lessonMapper.selectOverviewByType(lessonType);
        return refund == null ? Message.fail("无记录") : Message.success(null).add(refund);
    }

    @Override
    public Message<List<Lesson>> selectOverviewByDistrict(String district) {
        List<Lesson> refund = lessonMapper.selectOverviewByDistrict(district);
        return refund == null ? Message.fail("无记录") : Message.success(null).add(refund);
    }

    @Override
    public Message<List<Lesson>> selectOverviewByCampus(String campusName) {
        List<Lesson> refund = lessonMapper.selectOverviewByCampus(campusName);
        return refund == null ? Message.fail("无记录") : Message.success(null).add(refund);
    }

    @Override
    public Message<List<Lesson>> selectByHead(int head) {
        List<Lesson> refund = lessonMapper.selectByHead(head);
        return refund == null ? Message.fail("无记录") : Message.success(null).add(refund);
    }

    @Override
    public Lesson selectByPrimaryKey2(Integer id) {
        return lessonMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Lesson> selectAll2() {
        return lessonMapper.selectAll();
    }

    @Override
    public List<Lesson> selectByHead2(int head) {
        return lessonMapper.selectByHead(head);
    }

    @Override
    public int insertSelective(Lesson record) {
        return lessonMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(Lesson record) {
        return lessonMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Message<List<Lesson>> selectByLessonIdAndLessonNameAndCampusAndLessonType2(Integer lessonId, String lessonName, Integer campus, String lessonType,Integer userId) {
        List<Lesson> lessonList1 = new LinkedList<>();
        ConsoleUser consoleUser =consoleUserMapper.selectByPrimaryKey(userId);

        if(consoleUser.getUserGroup()==1){
            List<Lesson> lessonList =lessonMapper.selectByLessonIdAndLessonNameAndCampusAndLessonType2(lessonId, lessonName, campus, lessonType);
            for (int i=0;i<lessonList.size();i++)
            {
                Lesson lesson = lessonList.get(i);
                lesson.setCampusName(campusMapper.selectCampusNameByCampusId(lesson.getCampus()));
                lesson.setTypeName(lessonTypeMapper.selectTypeNameById((Integer.parseInt(lesson.getLessonType()))));
                lessonList1.add(lesson);
            }
        }else if(consoleUser.getUserGroup()==2){
            List<Lesson> lessonList =lessonMapper.selectByLessonIdAndLessonNameAndCampusAndLessonType3(lessonId, lessonName, campus, lessonType,userId);
            for (int i=0;i<lessonList.size();i++)
            {
                Lesson lesson = lessonList.get(i);
                lesson.setCampusName(campusMapper.selectCampusNameByCampusId(lesson.getCampus()));
                lesson.setTypeName(lessonTypeMapper.selectTypeNameById((Integer.parseInt(lesson.getLessonType()))));
                lessonList1.add(lesson);
            }
        }else if(consoleUser.getUserGroup()==3){
            Integer orgId=consoleUser.getOrgId();
            List<Campus> campusList= campusMapper.selectByOrgId(orgId);
            for(Campus campus1 :campusList){
                List<Lesson> lessonList =lessonMapper.selectByLessonIdAndLessonNameAndCampusAndLessonType5(lessonId, lessonName, campus, lessonType,campus1.getHead());
                for (int i=0;i<lessonList.size();i++)
                {
                    Lesson lesson = lessonList.get(i);
                    lesson.setCampusName(campusMapper.selectCampusNameByCampusId(lesson.getCampus()));
                    lesson.setTypeName(lessonTypeMapper.selectTypeNameById((Integer.parseInt(lesson.getLessonType()))));
                    lessonList1.add(lesson);
                }
            }
        }else if(consoleUser.getUserGroup()==4){
            List<Lesson> lessonList =lessonMapper.selectByLessonIdAndLessonNameAndCampusAndLessonType5(lessonId, lessonName, campus, lessonType,userId);
            for (int i=0;i<lessonList.size();i++)
            {
                Lesson lesson = lessonList.get(i);
                lesson.setCampusName(campusMapper.selectCampusNameByCampusId(lesson.getCampus()));
                lesson.setTypeName(lessonTypeMapper.selectTypeNameById((Integer.parseInt(lesson.getLessonType()))));
                lessonList1.add(lesson);
            }
        }
        return Message.success(null).add(lessonList1);
    }

    @Override
    public Message<List<Lesson>> selectByLessonIdAndLessonNameAndCampusAndLessonType(String str,Integer userId) {
        List<Lesson> lessonList1 = new LinkedList<>();
        ConsoleUser consoleUser=consoleUserMapper.selectByPrimaryKey(userId);

        if(consoleUser.getUserGroup()==1){
            List<Lesson> lessonList =lessonMapper.selectByLessonIdAndLessonNameAndCampusAndLessonType(str);
            for (int i=0;i<lessonList.size();i++)
            {
                Lesson lesson = lessonList.get(i);
                lesson.setCampusName(campusMapper.selectCampusNameByCampusId(lesson.getCampus()));
                lesson.setTypeName(lessonTypeMapper.selectTypeNameById((Integer.parseInt(lesson.getLessonType()))));
                lessonList1.add(lesson);
            }
        }else if(consoleUser.getUserGroup()==2){
            List<Lesson> lessonList =lessonMapper.selectByLessonIdAndLessonNameAndCampusAndLessonType4(userId,str);
            for (int i=0;i<lessonList.size();i++)
            {
                Lesson lesson = lessonList.get(i);
                lesson.setCampusName(campusMapper.selectCampusNameByCampusId(lesson.getCampus()));
                lesson.setTypeName(lessonTypeMapper.selectTypeNameById((Integer.parseInt(lesson.getLessonType()))));
                lessonList1.add(lesson);
            }
        }else if(consoleUser.getUserGroup()==3){
            Integer orgId=consoleUser.getOrgId();
            List<Campus> campusList= campusMapper.selectByOrgId(orgId);
            for(Campus campus : campusList){
                List<Lesson> lessonList =lessonMapper.selectByLessonIdAndLessonNameAndCampusAndLessonType6(campus.getHead(),str);
                for (int i=0;i<lessonList.size();i++)
                {
                    Lesson lesson = lessonList.get(i);
                    lesson.setCampusName(campusMapper.selectCampusNameByCampusId(lesson.getCampus()));
                    lesson.setTypeName(lessonTypeMapper.selectTypeNameById((Integer.parseInt(lesson.getLessonType()))));
                    lessonList1.add(lesson);
                }
            }

        }else if(consoleUser.getUserGroup()==4){
            List<Lesson> lessonList =lessonMapper.selectByLessonIdAndLessonNameAndCampusAndLessonType6(userId,str);
            for (int i=0;i<lessonList.size();i++)
            {
                Lesson lesson = lessonList.get(i);
                lesson.setCampusName(campusMapper.selectCampusNameByCampusId(lesson.getCampus()));
                lesson.setTypeName(lessonTypeMapper.selectTypeNameById((Integer.parseInt(lesson.getLessonType()))));
                lessonList1.add(lesson);
            }
        }
        return Message.success(null).add(lessonList1);
    }

    @Override
    public Message mohuConditionSearch(LessonSearch c) {



        if(c.getOrgId()!=null){
            List<Lesson> lessonList10 =lessonMapper.selectByLessonNameAndCampusAndUserId(c);
            List<Lesson> lessonList =new LinkedList<>();
            List<Campus> campusList = campusMapper.selectByOrgId(c.getOrgId());

            for (Campus campus:campusList){
                for (Lesson lesson:lessonList10){
                    if (lesson.getCampus()==campus.getCampusId()){
                        lessonList.add(lesson);
                    }
                }

            }
            for (Lesson lesson:lessonList){
                lesson.setCampusName(campusMapper.selectCampusNameByCampusId(lesson.getCampus()));
            }




            List<Lesson> lessonList1 = new LinkedList<>();
            if (lessonList.size()>0){
                if (c.getMinPrice()!=null)
                {
                    if (c.getMaxPrice()!=null)
                    {
                        for (Lesson lesson:lessonList)
                        {
                            int price = lesson.getLessonPrice().intValue()/100;
                            if (c.getMinPrice()<=price&&price<=c.getMaxPrice())
                            {
                                lessonList1.add(lesson);
                            }
                        }
                        return Message.success(null).add(lessonList1);
                    }else if (c.getMaxPrice()==null){
                        for (Lesson lesson:lessonList)
                        {
                            int price = lesson.getLessonPrice().intValue()/100;
                            if (c.getMinPrice()<=price)
                            {
                                lessonList1.add(lesson);
                            }
                        }
                        return Message.success(null).add(lessonList1);
                    }
                }else if (c.getMinPrice()==null&&c.getMaxPrice()!=null)
                {
                    for (Lesson lesson:lessonList)
                    {
                        int price = lesson.getLessonPrice().intValue()/100;
                        if (price<=c.getMaxPrice())
                        {
                            lessonList1.add(lesson);
                        }
                    }
                    return Message.success(null).add(lessonList1);
                }
                return  Message.success(null).add(lessonList);
            }
            return  Message.success(null).add(lessonList);




        }

        else{


        List<Lesson> lessonList =lessonMapper.selectByLessonNameAndCampusAndUserId(c);
        for (Lesson lesson:lessonList){
            lesson.setCampusName(campusMapper.selectCampusNameByCampusId(lesson.getCampus()));
        }




        List<Lesson> lessonList1 = new LinkedList<>();
        if (lessonList.size()>0){
            if (c.getMinPrice()!=null)
            {
                if (c.getMaxPrice()!=null)
                {
                    for (Lesson lesson:lessonList)
                    {
                        int price = lesson.getLessonPrice().intValue()/100;
                        if (c.getMinPrice()<=price&&price<=c.getMaxPrice())
                        {
                            lessonList1.add(lesson);
                        }
                    }
                    return Message.success(null).add(lessonList1);
                }else if (c.getMaxPrice()==null){
                    for (Lesson lesson:lessonList)
                    {
                        int price = lesson.getLessonPrice().intValue()/100;
                        if (c.getMinPrice()<=price)
                        {
                            lessonList1.add(lesson);
                        }
                    }
                    return Message.success(null).add(lessonList1);
                }
            }else if (c.getMinPrice()==null&&c.getMaxPrice()!=null)
            {
                    for (Lesson lesson:lessonList)
                {
                    int price = lesson.getLessonPrice().intValue()/100;
                    if (price<=c.getMaxPrice())
                    {
                        lessonList1.add(lesson);
                    }
                }
                return Message.success(null).add(lessonList1);
            }
            return  Message.success(null).add(lessonList);
        }
        }
        List<Lesson> lessonLists = new LinkedList<>();
        return  Message.success(null).add(lessonLists);
    }

    @Override
    public Message getAllByOrgId(int orgId) {
        List<Lesson> lessonList = new LinkedList<>();
        List<Campus> campusList = campusMapper.selectByOrgId(orgId);
        for (Campus campus:campusList){
            List<Lesson> lessonList1 = lessonMapper.selectByCampus(campus.getCampusId());
            for (Lesson lesson:lessonList1){
                lesson.setCampusName(campusMapper.selectCampusNameByCampusId(lesson.getCampus()));
                lessonList.add(lesson);
            }
        }


        return Message.success(null).add(lessonList);
    }
}





